package Com.Ring.TradingMobile.SQLite;

import Com.Ring.TradingMobile.Object.DanhMuc;
import Com.Ring.TradingMobile.Object.ItemDanhMuc;
import Com.Ring.TradingMobile.Object.TableName;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.CharArrayBuffer;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.os.Bundle;

public class DBManager {
	private Context mContext;
	private static final String DataBaseName = "RTStockData.db";
	private static final int Version = 2;
	private TableName Table;

	private DatabaseHelper mDbHelper;
	private SQLiteDatabase dbData;

	private static String DATABASE_CREATE_TABLE = "";

	public DBManager(Context context, TableName table) {
		mContext = context;
		Table = table;
		switch (Table) {
		case DanhMuc:
			DATABASE_CREATE_TABLE = "create table "+DanhMuc.TABLE_NAME+" (_id integer primary key autoincrement,"
					+ DanhMuc.NAME_DANH_MUC + " text not null);";
			break;
		case ItemDanhMuc:
			DATABASE_CREATE_TABLE = "create table "+ItemDanhMuc.TABLE_NAME+" (_id integer primary key autoincrement, "
					+ ItemDanhMuc.CODE_ITEM_DM
					+ " text not null, "
					+ ItemDanhMuc.ID_DANH_MUC + " integer);";
			break;
		}
		Open();
	}

	public Cursor Select(TableName table, String condition,String[] columns) {
		switch(table){
		case DanhMuc:
			return  SelectDanhMuc(condition, columns);
		case ItemDanhMuc:
			return null;
		}
		return null;
	}
	
	

	public void Insert(TableName table, Object... obj) {
		switch (table) {
		case DanhMuc:
			InsertDanhMuc(obj);
			break;
		case ItemDanhMuc:

			break;
		}
	}

	public void Delete(TableName table,Object... obj) {
		switch (table) {
		case DanhMuc:
			DeleteDanhMuc(obj);
			break;
		case ItemDanhMuc:

			break;
		}
	}

	public void Update() {

	}



	public SQLiteDatabase Read() {
		dbData = mDbHelper.getReadableDatabase();
		return dbData;
	}

	public SQLiteDatabase Write() {
		dbData = mDbHelper.getWritableDatabase();
		return dbData;
	}
	
	private Cursor SelectDanhMuc(String condition, String[] columns){
		ChangeStateSQLNeedRead();
		return dbData.query(DanhMuc.TABLE_NAME, columns, null, null, null, null, null);
	}

	private long InsertDanhMuc(Object... obj) {
		ContentValues values =new ContentValues();
		for (int i = 0; i < obj.length; i++) {
			values.put(DanhMuc.NAME_DANH_MUC, obj[i].toString());
		}
		if(values.size()>0){
			ChangeStateSQLNeedWrite();
			return dbData.insert(DanhMuc.TABLE_NAME, null, values);
		}
		return -2;
	}
	
	private void DeleteDanhMuc(Object... obj){
		ChangeStateSQLNeedWrite();
		String where = DanhMuc.NAME_DANH_MUC +" = ?";
		String[] args = new String[] {obj[0].toString()};
		dbData.delete(DanhMuc.TABLE_NAME, where, args);
	}
	
	protected void ChangeStateSQLNeedRead(){
		if(dbData!=null && dbData.isOpen()){
			if(!dbData.isReadOnly()){
			dbData.close();
			dbData = mDbHelper.getReadableDatabase();
			}
		}else{
			dbData = mDbHelper.getReadableDatabase();
		}
	}
	protected void ChangeStateSQLNeedWrite(){
		if(dbData!=null && dbData.isOpen()){
			if(dbData.isReadOnly()){
			dbData.close();
			dbData = mDbHelper.getWritableDatabase();
			}
		}else{
			dbData = mDbHelper.getWritableDatabase();
		}
	}
	
	
	protected void Open() {
		mDbHelper = new DatabaseHelper(mContext, DataBaseName, null, Version);
	}
	private static class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
			// TODO Auto-generated constructor stub

		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// check exist table//

			db.execSQL(DATABASE_CREATE_TABLE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub

		}
	}

}
